
package com.yuan.unit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JsonUtils {

    /**
     * 将Json字符串转化为Map<String, Object>
     * 
     * @param jsonString json字符串
     * @return 所转化为的Map对象 &nbsp;*&nbsp; @author YUANWEi &nbsp;
     */
    public static HashMap<String, Object> getMap(String jsonString) {
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(jsonString);
            @SuppressWarnings("unchecked")
            Iterator<String> keyIter = jsonObject.keys();
            String key;
            Object value;
            HashMap<String, Object> valueMap = new HashMap<String, Object>();
            while (keyIter.hasNext()) {
                key = (String) keyIter.next();
                value = jsonObject.get(key);
                valueMap.put(key, value);
            }
            return valueMap;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将Json字符串转化为List<Map<String, Object>>
     * 
     * @param jsonString json字符串
     * @return 所转化为的List<Map<String, Object>>对象
     * @throws JSONException
     */
    public static ArrayList<Map<String, Object>> getList(String jsonString) throws JSONException {
        ArrayList<Map<String, Object>> list = null;
        JSONObject temp = new JSONObject(jsonString);
        JSONArray jsonArray = temp.getJSONArray("STR");
        JSONObject jsonObject;
        list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            list.add(getMap(jsonObject.toString()));
        }
        return list;
    }

    /**
     * 获取第一个jsonArray的Map对象
     * 
     * @param jsonString
     * @return
     * @throws JSONException
     */
    public static HashMap<String, Object> getMap0(String jsonString) throws JSONException {
        List<HashMap<String, Object>> list = null;
        JSONObject temp = new JSONObject(jsonString);
        JSONArray jsonArray = temp.getJSONArray("STR");
        JSONObject jsonObject;
        list = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            list.add(getMap(jsonObject.toString()));
        }
        return list.get(0);
    }

    /**
     * 将List<Map<String, Object>>字符串转化为Json
     * 
     * @param List <Map<String, Object>>对象
     * @return 所转化为的jsonString json字符串
     */
    public static String LsittoJson(List<Map<String, Object>> list)
            throws Exception {
        JSONArray array = new JSONArray();
        JSONObject member = null;
        for (Map<String, Object> map : list) {
            member = new JSONObject();
            Set<Map.Entry<String, Object>> sets = map.entrySet();
            for (Map.Entry<String, Object> entry : sets) {
                member.put((String) entry.getKey(), (Object) entry.getValue());
                array.put(member);
            }
        }
        return array.toString();
    }

}
